package com.basic.business.indicatorsFile.controller;


import com.alibaba.fastjson2.JSONObject;
import com.basic.business.goodsIndicators.service.ITGoodsIndicatorsService;
import com.basic.business.indicatorsFile.domain.TIndicatorsFile;
import com.basic.business.indicatorsFile.service.ITIndicatorsFileService;
import com.basic.business.storeIndicators.service.ITStoreIndicatorsService;
import com.basic.business.unIndicators.service.ITUnboundedIndicatorsService;
import com.basic.business.wholeIndicators.service.ITWholeStationIndicatorsService;
import com.basic.common.annotation.FileExport;
import com.basic.common.annotation.Log;
import com.basic.common.config.BasicConfig;
import com.basic.common.constant.Constants;
import com.basic.common.core.controller.BaseController;
import com.basic.common.core.domain.AjaxResult;
import com.basic.common.core.page.TableDataInfo;
import com.basic.common.enums.BusinessType;
import com.basic.common.utils.StringUtils;
import com.basic.common.utils.easyExcel.EasyExcelUtils;
import com.basic.common.utils.file.FileUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 指标数据文件管理Controller
 *
 * @author zhaoxy_jn@163.com
 */
@RestController
@RequestMapping("/business/indicatorsFile")
public class TIndicatorsFileController extends BaseController
{
    @Resource
    private ITIndicatorsFileService tIndicatorsFileService;

    @Resource
    private ITStoreIndicatorsService tStoreIndicatorsService;

    @Resource
    private ITGoodsIndicatorsService tGoodsIndicatorsService;

    @Resource
    private ITWholeStationIndicatorsService tWholeStationIndicatorsService;

    @Resource
    private ITUnboundedIndicatorsService tUnboundedIndicatorsService;

    /**
     * 查询指标数据文件管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:indicatorsFile:list')")
    @GetMapping("/list")
    public TableDataInfo list(TIndicatorsFile tIndicatorsFile)
    {
        startPage();
        List<TIndicatorsFile> list = tIndicatorsFileService.getList(tIndicatorsFile);
        return getDataTable(list);
    }

    /**
     * 导出指标数据文件管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:indicatorsFile:export')")
    @Log(title = "指标数据文件管理", businessType = BusinessType.EXPORT)
    @FileExport(fileName = "指标数据文件管理.xlsx")
    @PostMapping("/export")
    public String export(TIndicatorsFile tIndicatorsFile) throws IOException
    {
        List<TIndicatorsFile> list = tIndicatorsFileService.getList(tIndicatorsFile);
        return EasyExcelUtils.excelExport(TIndicatorsFile.class,"指标数据文件管理数据", list);
    }

    /**
     * 获取指标数据文件管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:indicatorsFile:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tIndicatorsFileService.get(id));
    }

    /**
     * 新增指标数据文件管理
     */
    @PreAuthorize("@ss.hasPermi('business:indicatorsFile:add')")
    @Log(title = "指标数据文件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TIndicatorsFile tIndicatorsFile)
    {
        return toAjax(tIndicatorsFileService.insert(tIndicatorsFile));
    }

    /**
     * 修改指标数据文件管理
     */
    @PreAuthorize("@ss.hasPermi('business:indicatorsFile:edit')")
    @Log(title = "指标数据文件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TIndicatorsFile tIndicatorsFile)
    {
        return toAjax(tIndicatorsFileService.update(tIndicatorsFile));
    }

    /**
     * 删除指标数据文件管理
     */
    @PreAuthorize("@ss.hasPermi('business:indicatorsFile:remove')")
    @Log(title = "指标数据文件管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tIndicatorsFileService.deleteByIds(ids));
    }

    /**
     * 文件上传判断是否存在
     * @param hashCode
     * @return
     */
    @GetMapping("/isExistByHash")
    public AjaxResult isExistByHash(String hashCode) {

        int count = tIndicatorsFileService.getCountByFileHash(hashCode);

        if (count > 0) {
            //文件存在
            return AjaxResult.success(true);
        }{
            //文件不存在
            return AjaxResult.success(false);
        }
    }

    /**
     * 删除文件和数据
     * @param jsonObject
     * @return
     */
    @DeleteMapping("/deleteFileOrData")
    public AjaxResult deleteFileOrData(@RequestBody JSONObject jsonObject) {
        //文件id
        String id = jsonObject.getString("id");

        TIndicatorsFile file = tIndicatorsFileService.get(id);

        //删除文件以及数据
        switch (file.getType()){
            case 1:
                tStoreIndicatorsService.deleteByFileId(id);
                break;
            case 2:
                tGoodsIndicatorsService.deleteByFileId(id);
                break;
            case 3:
                tWholeStationIndicatorsService.deleteByFileId(id);
                break;
            case 4:
                tUnboundedIndicatorsService.deleteByFileId(id);
                break;
        }
        //删除服务器文件
        FileUtils.deleteFile(BasicConfig.getProfile() + StringUtils.substringAfter(file.getAbsolutePath(), Constants.RESOURCE_PREFIX));
        return toAjax(tIndicatorsFileService.delete(id));
    }
}
